﻿using Ops.Host.Common.Extensions;
using Ops.Host.Core.Models;

namespace Ops.Host.Core.Services.Impl;

internal sealed class SpotCheckService : ISpotCheckService
{
    private readonly IFreeSql _freeSql;

    public SpotCheckService(IFreeSql freeSql) => _freeSql = freeSql;

    public (List<SpotCheck> items, long count) GetPaged(SpotCheckFilter filter, int pageIndex, int pageItems)
    {
        var items = _freeSql.Select<SpotCheck>()
                            .WhereIf(!string.IsNullOrEmpty(filter.Station), s => s.Station == filter.Station)
                            .WhereIf(!string.IsNullOrEmpty(filter.Tooling), s => s.Tooling == filter.Tooling)
                            .WhereIf(!string.IsNullOrEmpty(filter.Item), s => s.Item == filter.Item)
                            .WhereIf(filter.IsPass != null, s => s.IsPass == filter.IsPass)
                            .WhereIf(!string.IsNullOrEmpty(filter.Shift), s => s.Shift == filter.Shift)
                            .WhereIf(filter.CreatedTimeStart != null, s => s.CreatedAt >= filter.CreatedTimeStart.ToDayMin())
                            .WhereIf(filter.CreatedTimeEnd != null, s => s.CreatedAt <= filter.CreatedTimeEnd.ToDayMax())
                            .OrderByDescending(s => s.CreatedAt)
                            .Count(out var total) //总记录数量
                            .Page(pageIndex, pageItems)
                            .ToList();
        return (items, total);
    }
}
